package com.clz.workorder.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.clz.workorder.BR
import com.clz.workorder.R
import com.clz.workorder.adapter.ComplaintAdapter
import com.clz.workorder.adapter.OrderAdapter
import com.clz.workorder.databinding.FragmentComplaintListBinding
import com.clz.workorder.databinding.FragmentOrderListBinding
import com.clz.workorder.viewmodel.ComplaintListViewModel
import com.clz.workorder.viewmodel.OrderListViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.config.ComplaintType
import com.czl.base.config.OrderType
import com.czl.base.event.LiveBusCenter

@Route(path = AppConstants.Router.Complaint.F_COMPLAINT_LIST)
class ComplaintListFragement: BaseFragment<FragmentComplaintListBinding, ComplaintListViewModel>()  {

    private lateinit var mAdapter: ComplaintAdapter
    override fun initContentView(): Int {
        return R.layout.fragment_complaint_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }
    override fun initData() {
        if (arguments != null) {
            viewModel.propertyState =
                requireArguments().getString(AppConstants.BundleKey.PROPERTY_STATE).toString()
        }
        initAdapter()
        reload()
    }
    private fun initAdapter() {
        mAdapter = ComplaintAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }
    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.current == data?.pages
            )
        })
        LiveBusCenter.observeRefreshComplaintEvent(this,{
            when(it.msg){
                ComplaintType.TAKE->{
                    when(viewModel.propertyState){
                        "0","1"->reload()
                    }
                }
                ComplaintType.HANDLE->{
                    when(viewModel.propertyState){
                        "1","2"->reload()
                    }
                }
                ComplaintType.TURN ->{
                    when(viewModel.propertyState){
                        "0","1"->reload()
                    }
                }
            }
        })
    }
    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }
}